package com.addremoveitemtest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

/**
 * Created by twopai on 2017/3/30.
 */

public class AddRemoveAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<AddRemoveEntity>addList1;
    private List<AddRemoveEntity>addListSum;
    public static final int TYPE0=0;
    public static final int TYPE1=1;
    public static final int TYPE2=2;

    public AddRemoveAdapter(Context context, List<AddRemoveEntity> addList1, List<AddRemoveEntity> addListSum) {
        this.context = context;
        this.addList1 = addList1;
        this.addListSum = addListSum;
    }
    private View.OnClickListener onClickListener;

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        RecyclerView.ViewHolder viewHolder=null;
        if (viewType == TYPE0) {
            MyViewHolder01 myViewHolder01 = new MyViewHolder01(creatTextView("选择item"));
            return myViewHolder01;
        }else if (viewType == TYPE1) {
            MyViewHolder01 myViewHolder02= new MyViewHolder01(creatTextView("总的item"));
            return myViewHolder02;
        }else {
            View view = layoutInflater.inflate(R.layout.item_interest, parent, false);
            MyViewHolder2 myViewHolder2 = new MyViewHolder2(view);
            return myViewHolder2;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            //进行"选择item"标题操作
        }else if (position == addList1.size()+1) {
            //进行"总的item"标题操作
        }else {
            MyViewHolder2 holder2 = (MyViewHolder2) holder;
            holder2.btnItem.setText(addListSum.get(position).getName());
            /*在此处设置对应item的标记，并绑定监听*/
            holder2.btnItem.setTag(addListSum.get(position).getId());
            holder2.btnItem.setOnClickListener(onClickListener);
        }
    }


    /**
     * 设置相应item的对应view样式的标记
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE0;//可以看作"选择item"的标记
        }else if (position == addList1.size()+1) {
            return TYPE1;//可以看作"总的item"的标记
        }else {
            return TYPE2;//可以看作上下集合的标记
        }
    }

    @Override
    public int getItemCount() {
        return addListSum.size();
    }
    public TextView creatTextView(String text){
        TextView textView = new TextView(context);
        textView.setTextSize(16f);
        textView.setText(text);
        return textView;
    }

    class MyViewHolder01 extends RecyclerView.ViewHolder{
        public MyViewHolder01(View itemView) {
            super(itemView);
        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder{
        private final Button btnItem;
        public MyViewHolder2(View itemView) {
            super(itemView);
            btnItem = (Button) itemView.findViewById(R.id.btn_label);
        }
    }
}
